package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @author ：zfr
 * @date ：2021/2/1 18:10
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_CHANGE_TRANSPORT, desc = "更换交通工具")
public class ChangeTransportCmd extends AbstractCommand {
    private int transportId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.ChangeTransportReqMsg msg = HuaShiGangProto.ChangeTransportReqMsg.parseFrom(packet.getBytes());
        transportId = msg.getTransportId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //活动检验
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        //检验活动是否过期
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        // 配置不开启宝马功能
        if (HuaShiGangMgr.isUseMount != 1) {
            ret = GameErrorCode.E_HUA_SHI_GANG_MOUNT_NOT_OPEN;
            return;
        }
        // 暂时只有宝马 没有配置id 以1代替 0为默认 判断一下id合法性
        if (transportId < 0 || transportId > 1) {
            ret = GameErrorCode.E_HUA_SHI_GANG_TRANSPORT_ID_ILLEGAL;
            return;
        }
        HuaShiGangProto.CrossChangeTransportReqMsg.Builder builder = HuaShiGangProto.CrossChangeTransportReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setTransportId(transportId);
        player.sendPacket(Protocol.C2_HUA_SHI_GANG_CHANGE_TRANSPORT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.ChangeTransportRespMsg.Builder builder = HuaShiGangProto.ChangeTransportRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_CHANGE_TRANSPORT, builder);
        }
    }
}
